﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;
using Web.Utils;

namespace Web.Page
{
    public partial class DepartmentEdit : System.Web.UI.Page
    {
        protected string id;
        protected string pid;
        protected string pcode;
        protected Department dept;
        protected string resultMessage = "";

        protected  Departments_Dao dao = new Departments_Dao();

        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"];
            pid = Request.QueryString["pid"];
            pcode = Request.QueryString["pcode"];

            if (!IsPostBack)
            {
                if (id == null)
                {
                    this.lblTitle.Text = "新增部门";
                }
                else
                {
                    this.lblTitle.Text = "修改部门";
                    dept = dao.Query(id);
                    setFields();
                    setControls();
                }

                btnDel.Attributes.Add("onclick ", "return   confirm( '确定删除？ ') ");
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (id == null)
            {
                add();
            }
            else
            {
                update();
            }

            this.lbResult.Text = resultMessage;
        }

        private void add()
        {
            dept = new Department()
            {
                Id = Guid.NewGuid(),
            };
            getFields();

            if (pid != null)
            {
                dept.ParentId = Guid.Parse(pid);
                //dept.Code = CodeHelper.GenDepartmentCode(pcode, dept.Code);
            }
            
            try
            {
                int result = dao.Add(dept);
                if (result == 1)
                {
                    resultMessage = "添加部门成功";
                }
            }
            catch (Exception ex)
            {
                resultMessage = "添加部门失败:" + ex.Message;
            }
        }

        private void update()
        {
            dept = dao.Query(id);
            getFields();

            try
            {
                int result = dao.Update(dept);
                if (result == 1)
                {
                    resultMessage = "更新部门成功";
                }
            }
            catch (Exception ex)
            {
                resultMessage = "更新部门失败:" + ex.Message;
            }
        }

        private void setFields()
        {
            this.tbCode.Enabled = false;
            this.tbCode.Text = dept.Code.Trim();
            this.tbName.Text = dept.Name;
            this.tbDescription.Text = dept.Description;
        }

        private void getFields()
        {
            dept.Code = this.tbCode.Text;
            dept.Name = this.tbName.Text;
            dept.Description = this.tbDescription.Text;
        }

        private void setControls()
        {
            hlAddDepartment.NavigateUrl = string.Format("DepartmentEdit.aspx?pid={0}", id);
            hlAddUser.NavigateUrl = string.Format("UserAdd.aspx?pid={0}", id);
        }

        private void ClearControlsValue()
        {
            this.lbResult.Text = "";
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ClearControlsValue();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            AsyncPostBackTrigger outsideTrigger = new AsyncPostBackTrigger();
            outsideTrigger.ControlID = "btnSave";
            outsideTrigger.EventName = "Click";
            this.UpdatePanel2.Triggers.Add(outsideTrigger);
        }

        protected void btnDel_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                int result = dao.Delete(id);
                if (result == 1)
                {
                    resultMessage = "删除部门成功";
                }
            }
            catch (Exception ex)
            {
                resultMessage = "删除部门失败:" + ex.Message;
            }
        }
    }
}